<?php
/**
 * Defines the editing form for the formulas question type.
 *
 * @copyright &copy; 2010 Hon Wai, Lau
 * @author Hon Wai, Lau <lau65536@gmail.com>
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License version 3
 * @package questionbank
 * @subpackage questiontypes
 */

require_once($CFG->dirroot.'/question/type/edit_question_form.php');

/**
 * coodinate question type editing form definition.
 */
class question_edit_formulas_form extends question_edit_form {
    
    /**
    * Add question-type specific form fields.
    *
    * @param MoodleQuickForm $mform the form being built.
    */
    function definition_inner(&$mform) {
        $mform->addElement('header','globalvarshdr',get_string('globalvarshdr','qtype_formulas'));

        $mform->removeElement('defaultgrade');        
        $mform->addElement('hidden', 'defaultgrade');        
        $mform->setType('defaultgrade', PARAM_RAW);
        
        $mform->removeElement('penalty');        
        $mform->addElement('hidden', 'penalty');        
        $mform->setType('penalty', PARAM_NUMBER);
        $mform->setDefault('penalty', 0.1);
        
        $mform->addElement('static', 'help_formulas', get_string('help'),
            get_string('helponquestionoptions', 'qtype_formulas'));
            
        $mform->addElement('textarea', 'varsrandom', get_string('varsrandom', 'qtype_formulas'),
            array('rows' => 4, 'cols' => 70, 'course' => $this->coursefilesid));
            
        $mform->addElement('textarea', 'varsglobal', get_string('varsglobal', 'qtype_formulas'),
            array('rows' => 6, 'cols' => 70, 'course' => $this->coursefilesid));

        $mform->addElement('select', 'showperanswermark', get_string('showperanswermark', 'qtype_formulas'),
            array(get_string('choiceno', 'qtype_formulas'), get_string('choiceyes', 'qtype_formulas')));
        $mform->setDefault('showperanswermark', 1);
        
        $mform->addElement('select', 'peranswersubmit', get_string('peranswersubmit', 'qtype_formulas'),
            array(get_string('choiceno', 'qtype_formulas'), get_string('choiceyes', 'qtype_formulas')));
        $mform->setDefault('peranswersubmit', 1);
        
        $mform->addElement('text', 'retrymarkseq', get_string('retrymarkseq', 'qtype_formulas'),
            array('size' => 30));
        
        $creategrades = get_grade_options();
        $this->add_per_answer_fields($mform, get_string('answerno', 'qtype_formulas', '{no}'),
            $creategrades->gradeoptions, 1, 1);
    }
    
    
    /**
    * Add the answer field for a particular subquestion labelled by placeholder.
    * 
    * @param MoodleQuickForm $mform the form being built.
    */
    function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
        $repeated = array();
        $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
        
        $repeated[] =& $mform->createElement('text', 'placeholder', get_string('placeholder', 'qtype_formulas'),
            array('size' => 20));
        $repeated[] =& $mform->createElement('htmleditor', 'subqtext', get_string('subqtext', 'qtype_formulas'),
            array('course' => $this->coursefilesid));
        $repeated[] =& $mform->createElement('textarea', 'feedback', get_string('feedback', 'qtype_formulas'),
            array('rows' => 4, 'cols' => 70, 'course' => $this->coursefilesid));
        
        $repeated[] =& $mform->createElement('static', '', '<hr class="formulas_seperator1">', '');
        $repeated[] =& $mform->createElement('text', 'answermark', get_string('answermark', 'qtype_formulas'),
            array('size' => 3));
        $repeated[] =& $mform->createElement('textarea', 'vars1', get_string('vars1', 'qtype_formulas'),
            array('rows' => 5, 'cols' => 70, 'course' => $this->coursefilesid));
        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'qtype_formulas'),
            array('size' => 60));
        $repeated[] =& $mform->createElement('textarea', 'vars2', get_string('vars2', 'qtype_formulas'),
            array('rows' => 5, 'cols' => 70, 'course' => $this->coursefilesid));
        $repeated[] =& $mform->createElement('text', 'correctness', get_string('correctness', 'qtype_formulas'),
            array('size' => 60));
        
        $repeated[] =& $mform->createElement('static', '', '<hr class="formulas_seperator2">', '');
        $repeated[] =& $mform->createElement('text', 'unitpenalty', get_string('unitpenalty', 'qtype_formulas'),
            array('size' => 3));
        $repeatedoptions['unitpenalty']['default'] = 1;
        $repeated[] =& $mform->createElement('text', 'postunit', get_string('postunit', 'qtype_formulas'),
            array('size' => 60));
        
        global $basic_unit_conversion_rules;
        foreach ($basic_unit_conversion_rules as $id => $entry)  $default_rule_choice[$id] = $entry[0];
        $repeated[] =& $mform->createElement('select', 'ruleid', get_string('ruleid', 'qtype_formulas'),
            $default_rule_choice);
        $repeatedoptions['ruleid']['default'] = 1;
        $repeated[] =& $mform->createElement('textarea', 'otherrule', get_string('otherrule', 'qtype_formulas'),
            array('rows' => 2, 'cols' => 70, 'course' => $this->coursefilesid));
        
        $answersoption = 'answers';
        return $repeated;
    }
    
    
    /**
    * Sets the existing values into the form for the question specific data.
    * It sets the answers before calling the parent function.
    *
    * @param $question the question object from the database being used to fill the form
    */
    function set_data($question) {
        if (isset($question->options)){
            $question->questiontext = '';   // replace the {#} in the questiontext by the named/unnamed placeholder
            foreach ($question->options->answers as $i => $answer)
                $question->questiontext .= $question->options->pretexts[$i]
                    . (strlen($answer->placeholder) == 0 ? '' : '{'.$answer->placeholder.'}');
            $question->questiontext .= $question->options->posttext;
            global $QTYPES;
            $extras = $QTYPES[$this->qtype()]->subquestion_option_extras();
            foreach ($extras as $extra)  $default_values[$extra] = $question->options->extra->$extra;
            if (count($question->options->answers)) {
                $tags = $QTYPES[$this->qtype()]->subquestion_answer_tags();
                foreach ($question->options->answers as $key => $answer) {
                    foreach ($tags as $tag)  $default_values[$tag.'['.$key.']'] = $answer->$tag;
                }
            }
            $question = (object)((array)$question + $default_values);
        }
        parent::set_data($question);
    }
    
    
    /**
    * Validating the data returning from the client.
    * 
    * The check the basic error as well as the formula error by evaluating one instantiation.
    */
    function validation($data, $files) {
        $errors = parent::validation($data, $files);
        
        global $QTYPES;
        $qt = & $QTYPES[$this->qtype()];
        
        // use the validation defined in the question type, check by instantiating one variable set
        $errorsofthisqtype = $QTYPES[$this->qtype()]->validation($data, $files);
        
        return array_merge($errors, $errorsofthisqtype);
    }


    function qtype() {
        return 'formulas';
    }
}
?>
